package com.hwl.eduservice.controller;


import com.hwl.commonutils.result.RespBean;
import com.hwl.commonutils.utils.JwtInfo;
import com.hwl.commonutils.utils.JwtUtils;
import com.hwl.eduservice.service.CourseCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-09-21
 */
@Api(tags = "课程收藏")
@RestController
@RequestMapping("/api/eduCollect/auth")
public class ApiCourseCollectController {

    @Resource
    private CourseCollectService courseCollectService;

    @ApiOperation("判断是否收藏")
    @GetMapping("isCollect/{courseId}")
    public RespBean isCollect(@PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        return RespBean.ok().data(courseCollectService.isCollect(courseId, jwtInfo.getId()));
    }

    @ApiOperation("收藏课程")
    @PostMapping("save/{courseId}")
    public RespBean save(@PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (courseCollectService.saveCourseCollect(courseId, jwtInfo.getId())) {
            return RespBean.ok().message("收藏成功！");
        }
        return RespBean.error().message("收藏失败！");
    }

    @ApiOperation("获取课程收藏列表")
    @GetMapping("list")
    public RespBean collectList(HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        return RespBean.ok().data(courseCollectService.selectListByMemberId(jwtInfo.getId()));
    }

    @ApiOperation("取消收藏课程")
    @DeleteMapping("remove/{courseId}")
    public RespBean remove(@PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (courseCollectService.removeCourseCollect(courseId, jwtInfo.getId())) {
            return RespBean.ok().message("取消收藏成功！");
        }
        return RespBean.error().message("取消收藏失败！");
    }

}

